
package de.godlike.android;

import de.godlike.android.data.DbHelper;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class JWebminActivity extends Activity implements OnClickListener, OnItemLongClickListener {

    private Dialog contextDialog = null;

    @SuppressWarnings("unused")
    private static final String TAG = "JWEBMIN";

    private ListView mainView;

    private DbHelper db;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Eventuell nicht UI Thread
        contextDialog = new Dialog(this);
        contextDialog.setContentView(R.layout.server);
        contextDialog.findViewById(R.id.btnServerSave).setOnClickListener(this);
        contextDialog.findViewById(R.id.btnServerAbort).setOnClickListener(this);
        
        db = new DbHelper(getApplicationContext());
        mainView = (ListView) findViewById(R.id.mainList);
        mainView.setOnItemLongClickListener(this);
        updateServerList();
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnServerSave:
                // TODO: read entries
                String serverUrl = ((EditText) contextDialog.findViewById(R.id.serverUrlEdit))
                        .getText().toString();
                String serverUser = ((EditText) contextDialog.findViewById(R.id.serverUsrEdit))
                        .getText().toString();
                String serverPwd = ((EditText) contextDialog.findViewById(R.id.serverPwdEdit))
                        .getText().toString();

                // TODO: check entries
                db.addServer(serverUrl, serverUser, serverPwd);
                contextDialog.cancel();
                updateServerList();
                resetDialog();
                break;
            case R.id.btnServerAbort:
                contextDialog.cancel();
                resetDialog();
                break;
            default:

                break;
        }
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.newServer:
                contextDialog.setTitle("Anlegen");
                contextDialog.show();
                break;
        }
        return true;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    // Stuff for handling dialog events
    public void resetDialog() {
        ((EditText) contextDialog.findViewById(R.id.serverPwdEdit))
                .setText(R.string.serverpasswordbsp);
        ((EditText) contextDialog.findViewById(R.id.serverUsrEdit)).setText(R.string.serveruserbsp);
        ((EditText) contextDialog.findViewById(R.id.serverUrlEdit)).setText(R.string.serverurlbsp);
    }

    public void updateServerList() {
        mainView.setAdapter(new SimpleCursorAdapter(this, R.layout.server_list_item, db
                .getAllServer(), new String[] {
                "URL", BaseColumns._ID
        }, new int[] {
                R.id.url, R.id.serverid
        }));
    }

	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		final TextView tv = (TextView) arg1.findViewById(R.id.serverid);
		final TextView tvServerUrl = (TextView) arg1.findViewById(R.id.url);
		
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage("Wollen Sie den Server (-->"+tvServerUrl.getText()+"<--) wirklich löschen?")
    	       .setCancelable(false)
    	       .setPositiveButton("Ja", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   db.removeServer(tv.getText().toString());
    	        	   updateServerList();
    	                dialog.dismiss();
    	           }
    	       })
    	       .setNegativeButton("Nein", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	                dialog.cancel();
    	           }
    	       });
    	
    	AlertDialog alert = builder.create();
    	alert.show();
		return true;
	}

}
